/**
 *====================================================
* 文件名称: ClaimServiceImpl.java
 * 修订记录：
  * No    日期				作者(操作:具体内容)
  * 1.    2015年12月09日	chenxy(创建:创建文件)
  *====================================================
  * 类描述：(说明未实现或其它不应生成javadoc的内容)
  */
package com.yuexunit.finance.project.service;

import java.math.BigDecimal;

import org.apache.commons.lang.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import com.yuexunit.finance.common.util.BeanUtils;
import com.yuexunit.finance.common.util.RandomUtil;
import com.yuexunit.finance.project.SettingConstants;
import com.yuexunit.finance.project.entity.ClaimEntity;
import com.yuexunit.finance.project.entity.lexicon.AbnormalStateEnum;
import com.yuexunit.finance.project.entity.lexicon.ClaimStateEnum;
import com.yuexunit.inject.annotation.Implements;
import com.yuexunit.page.PageCriteria;
import com.yuexunit.page.PageList;
import com.yuexunit.persist.PersistUtil;
import com.yuexunit.util.DateUtil;

/**
 * @ClassName: ClaimServiceImpl
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @author chenxy
 * @date 2015年12月09日 11:07:15
 */
@Implements(ClaimService.class)
public class ClaimServiceImpl implements ClaimService {

	@Override
	public Long createClaim(ClaimEntity entity) {
		entity.setIndemnityAmount(BigDecimal.ZERO);
		entity.setClaimState(ClaimStateEnum.CLAIM);
		entity.setClaimProcess(1);
		entity.setClaimNumber(SettingConstants.PRE_NUMBER_CLAIM + RandomUtil.generateTimeNumber());
		entity.setCreateDateTime(DateUtil.now());
		PersistUtil.insert(entity);
		return entity.getClaimId();
	}

	@Override
	public void updateClaim(ClaimEntity entity) {
		ClaimEntity persist = inquireClaim(entity.getClaimId());
		BeanUtils.copyProperties(entity, persist);
		PersistUtil.update(persist);
	}

	@Override
	public void renovateClaim(ClaimEntity persist) {
		PersistUtil.update(persist);
	}

	@Override
	public ClaimEntity inquireClaim(Long claimId) {
		return PersistUtil.get(ClaimEntity.class, claimId);
	}

	@Override
	public PageList<ClaimEntity> inquireClaimPageList(Long financeId, Integer beginDate, Integer endDate, String claimNumber, AbnormalStateEnum abnormalState, String insuranceCompany, Integer claimProcess, Integer claimState, PageCriteria page) {
		DetachedCriteria criteria = DetachedCriteria.forClass(ClaimEntity.class);
		if (financeId != null) {
			criteria.add(Restrictions.eq("financeId", financeId));
		}
		if (beginDate != null) {
			criteria.add(Restrictions.ge("claimDate", beginDate));
		}
		if (endDate != null) {
			criteria.add(Restrictions.le("claimDate", endDate));
		}
		if (StringUtils.isNotBlank(claimNumber)) {
			criteria.add(Restrictions.like("claimNumber", "%" + claimNumber + "%"));
		}
		if (abnormalState != null && abnormalState != AbnormalStateEnum.UNKNOWN) {
			criteria.add(Restrictions.eq("abnormalState", abnormalState));
		}
		if (insuranceCompany != null) {
			criteria.add(Restrictions.like("insuranceCompany", "%" + insuranceCompany + "%"));
		}
		if (claimProcess != null) {
			criteria.add(Restrictions.eq("claimProcess", claimProcess));
		}
		if (claimState != null) {
			criteria.add(Restrictions.eq("claimState", claimState));
		}
		return PersistUtil.get(criteria, page);
	}

}
